#ifndef TEXOPS_H_
#define TEXOPS_H_

#include "common.h"

// A tiny wrapper around glBindTexture(GL_TEXTURE_2D, tex);
//  this simply checks that tex is not already bound
//  and potentially saves unneccessary re-binds
inline void use_texture(GLuint tex);
// sets last_used_texture back to 0 (force texture bind)
inline void reset_texture();

// helper function: return next-highest PO2
int powerOfTwo( int );

// Uploads an existing SDL_Surface as a GL texture
//  supports transparency
GLuint build_texture(SDL_Surface *, GLuint filter);

// Loads any image file as a GL texture
//  supports transparency
GLuint load_texture(const char *, GLuint filter, unsigned int *w, unsigned int *h);
GLuint load_texture_colorkey(const char *, GLuint filter, unsigned char r,unsigned char g,unsigned char b, unsigned int *w, unsigned int *h);

// Like build_texture except calls glTexSubImage2D
void rebuild_texture (GLuint, SDL_Surface *);

// Loads a texture from a separated palette / image block
//  e.g. from a smackfile...
SDL_Surface *tex_from_smkframe(unsigned char *palette, unsigned char *image, int w, int h);

// replacement for glTexCoord2i
void glTC2i(GLuint x, GLuint y, GLuint w, GLuint h);

// draws a quad to a screen in glOrtho mode
void glBox(GLuint x, GLuint y, GLuint w, GLuint h, GLuint t_x, GLuint t_y, GLuint t_w, GLuint t_h);

// callback for timer
Uint32 next_frame(Uint32 interval, void *param);

// Renders a string using systemfont to an RGBA texture
//  if existing_texture is supplied, it is replaced
//  this allows to update existing dedicated text surfaces
struct font
{
	SDL_Surface *tex;
	struct rect
	{
		unsigned short x,y,w,h;
	} **r;
};

struct font *load_font(const char *defname);

GLuint render_text(const char *text, SDL_Color text_color, GLuint existing_texture );

void free_font(struct font *font);

#endif
